﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using TomShane.Neoforce.Controls;


namespace Engine.Scene
{
    public class BaseScene:DrawableGameComponent, Engine.Scene.IBaseScene
    {
        private string _name;
        private Manager _GUIManager;
        private bool _haveGUI;
        private string _skin;
        Dictionary<string, Interface.IEngineObject> _EngineObjects;
        Dictionary<string, Interface.IEngineObject> _GUIObjects;
        public string Name { get {return _name; } set { _name = value; } }
        public BaseScene(NOWAEngine _game,string name,bool haveGUI=false,string skin="Green"):base(_game)
        {       
            // TODO: Complete member initialization
            _name = name;
            _EngineObjects = new Dictionary<string, Interface.IEngineObject>(0);
            _GUIObjects = new Dictionary<string, Interface.IEngineObject>(0);
            _haveGUI = haveGUI;
            if (haveGUI)
            {
               // _GUIManager = new Manager(_game, skin);
                _GUIManager = new Manager(Game,_game.graphics, skin);
                _haveGUI = true;
                _skin = skin;


            }
        }
        public BaseScene(NOWAEngine _game, string name, Manager MangerGUI)
            : base(_game)
        {
            // TODO: Complete member initialization
            _name = name;
            _EngineObjects = new Dictionary<string, Interface.IEngineObject>(0);
            _GUIObjects = new Dictionary<string, Interface.IEngineObject>(0);
         
                // _GUIManager = new Manager(_game, skin);
                _haveGUI = true;
                _skin = MangerGUI.Skin.Name;
            _GUIManager=MangerGUI;


            
        }
        
        public void addObject(Interface.IEngineObject obj,SceneNode parent=null)
        {
            if (obj.isGUI)
            {
                if (_GUIObjects.ContainsKey(obj.Name))
                {
                    _GUIObjects[obj.Name] = obj;
                }
                else
                {
                    _GUIObjects.Add(obj.Name, obj);
                }
            }
            else
            {
                if (_EngineObjects.ContainsKey(obj.Name))
                {
                    _EngineObjects[obj.Name] = obj;
                }
                else
                {
                    _EngineObjects.Add(obj.Name, obj);
                }
            }
        }
        public void addGUIControl(Component component)
        {
            _GUIManager.Add(component);
           
        }
        public void RemoveObject(String Name)
        {
            if (_GUIObjects.ContainsKey(Name))
            {
                _GUIObjects.Remove(Name);
            }
          
        }
        public void RemoveGUI(Component component)
        {
            _GUIManager.Remove(component);
        }
        public dynamic getGUIByName(string name)
        {
            return _GUIManager.GetControl(name);
            //if (_GUIObjects.ContainsKey(name))
            //{
                
            //    return _GUIObjects[name];
            //}
            //return null;
        }
        public dynamic getObjectByName(string name)
        {
            if (_EngineObjects.ContainsKey(name))
            {
                return _EngineObjects[name];
            }
            return null;
        }
        public override void Initialize()
        {
            if (_haveGUI)
            {
               
                _GUIManager.Initialize();
            }
            foreach (var item in _EngineObjects)
            {
                item.Value.Initialize();
            }
            foreach (var item in _GUIObjects)
            {
                item.Value.Initialize();
            } 
            base.Initialize();
        }
        public override void Update(GameTime gameTime)
        {
            if (_haveGUI)
            {              
                _GUIManager.Update(gameTime);
            }
            foreach (var item in _EngineObjects)
            {
                item.Value.Update(gameTime);
            }
            foreach (var item in _GUIObjects)
            {
                item.Value.Update(gameTime);
            } base.Update(gameTime);
        }
        public override void Draw(GameTime gameTime)
        {
            if (_haveGUI)
            {
                
                _GUIManager.BeginDraw(gameTime);
                if (background!=Color.Transparent)
                {
                     Game.GraphicsDevice.Clear(background);
                }
               
               // _GUIManager.Draw(gameTime);
            }
            foreach (var item in _EngineObjects)
              {
                  item.Value.Draw(gameTime);
              }
              foreach (var item in _GUIObjects)
              {
                  item.Value.Draw(gameTime);
              }
              
              if (_haveGUI)
              {
                  _GUIManager.EndDraw();
              }
base.Draw(gameTime);
        }
        public virtual void LoadContent()
        {
          
            foreach (var item in _EngineObjects)
            {
                item.Value.LoadContent();
            }
            foreach (var item in _GUIObjects)
            {
                item.Value.LoadContent();
            }
            base.LoadContent();
        }

        public TomShane.Neoforce.Controls.Manager GUIManager
        {
            get { return _GUIManager; }
        }

        public bool HaveGUI
        {
            get
            {
                return _haveGUI;
            }
            set
            {
                _haveGUI=value;
            }
        }

        private Color background=Color.Transparent;
        public Color backColor
        {
            get
            {
                return background ;
            }
            set
            {
                background = value ;
            }
        }
    }
}
